package com.asset.dispoal.verdict.service.common;

import com.asset.dispoal.commom.page.PagingQuery;
import com.asset.dispoal.verdict.domain.db.entity.GbJudgmentBook;
import com.asset.dispoal.verdict.domain.db.mapper.GbJudgmentBookMapper;
import com.asset.dispoal.verdict.dto.request.JudgmentBookRequest;
import com.asset.dispoal.verdict.dto.response.JudgmentBookResponse;
import com.asset.dispoal.verdict.service.converter.GbJudgmentBookConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @description:
 * @author: yf
 * @date: 2022-09-06
 */
@Repository
public class QueryJudgmentCommand implements BaseCommand<JudgmentBookRequest, Result<PageVO<JudgmentBookResponse>>> {
    @Autowired
    private GbJudgmentBookMapper gbJudgmentBookMapper;

    @Override
    public Result<PageVO<JudgmentBookResponse>> execute(JudgmentBookRequest request) {
        GbJudgmentBook judgmentBookQo = GbJudgmentBookConverter.INSTANCE.reqToDoQo(request);
        judgmentBookQo.setPermission((String) GlobalHeaderThreadLocal.getOrException().getExtraMap().get("permission"));
        int count = gbJudgmentBookMapper.count(judgmentBookQo);
        Page<GbJudgmentBook> page = PagingQuery.getPage(count, judgmentBookQo);
        PageVO<JudgmentBookResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(count);

        if (page != null) {
            List<GbJudgmentBook> judgmentBooks = gbJudgmentBookMapper.queryAllByLimit(page);
            List<JudgmentBookResponse> bsCaseResponses = GbJudgmentBookConverter.INSTANCE.dosToRes(judgmentBooks);
            pageVO.setResult(bsCaseResponses);
        }
        return Result.ok(pageVO);
    }
}
